package Project1;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;


import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;

import Project1.Employee;

@Entity
@Table (name = "projects")
public class Projects {
	private int proj_num;
	private String proj_name;
	private Set<Employee> employees = new HashSet<Employee>();
	
	public Projects(){}; 
	public Projects(int proj_num, String proj_name) {
		this.proj_num = proj_num;
		this.proj_name = proj_name; 
	}
	
	//**************************GETTERS SETTERS***********************************
	@Id
	@Column(name="proj_num")
	public int getProj_num(){ return proj_num; } 
	public void setProj_num(int proj_num) { this.proj_num = proj_num; }
	
	@Column(name="proj_name")
	public String getProj_name(){return proj_name;}
	public void setProj_name (String proj_name){ this.proj_name = proj_name;}
	
	
    @ManyToMany (fetch = FetchType.EAGER)
    @JoinTable(name="employee_projects", 
 		joinColumns={@JoinColumn(name="proj_num")},
 		inverseJoinColumns={@JoinColumn(name="emp_no")})
    @Fetch(value = FetchMode.SELECT) 
    public Set<Employee> getEmployees() { return employees; }
    public void setEmployees(Set<Employee> employees) { this.employees = employees; }
    
    //***************METHODS***********************************
	public void print(){
		System.out.printf("%d : %s\n", proj_num, proj_name);
	}
	
	//******************STATIC METHODS*****************************
	/*
	 * Lists all Projects
	 */
	public static void list(){

		Session session = HibernateContext.getSession();
		Criteria criteria = session.createCriteria(Projects.class);
		criteria.addOrder(Order.asc("proj_num"));

		List<Projects> proj = criteria.list();
		System.out.println("All Projects: ");
	
		for(Projects p: proj){
			p.print();
		}
		session.close();
	}

	/*
	 * Finds project of specific name
	 */	
	public static Projects find(int proj_num){
	    	// Query using HQL.
	        Session session = HibernateContext.getSession();
	        Query query = session.createQuery("from Projects where proj_num = :idvar");
	        query.setLong("idvar", proj_num);
	        Projects p = (Projects) query.uniqueResult();
	       
	        session.close();
	        return p;
		}
	public static Projects find(String proj_name){
		Projects prototype = new Projects();
		prototype.setProj_name(proj_name);
		Example example = Example.create(prototype);
		
		Session session = HibernateContext.getSession();
		Criteria criteria = session.createCriteria(Projects.class);
		criteria.add(example);
		
		Projects proj = (Projects) criteria.uniqueResult();
		
		session.close();
		return proj;
	}
	/**
	 * add a project to projects table
	 */
	public static void add(int proj_num, String proj_name){
		Projects proj = new Projects (proj_num, proj_name);
		
		Session sess = HibernateContext.getSession();
		Transaction t = sess.beginTransaction();
		{
			sess.save(proj);
		}
		t.commit();
		sess.close();
		System.out.println("New project has been added");
	}
	
}
